{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 日历 calendar\n",
    "- import calendar\n",
    "- calendar.calendar(2018):2018 的日历 同：calendar.prcal(2018)\n",
    "- calendar.prmonth(2018,9) :打印2018.9月的日历\n",
    "- calendar.isleap(2018):判断2018是不是闰年\n",
    "- calendar.leapdays(1900,2018):判断1900-2018年的闰年个数\n",
    "- calendar.weekdays(2018,9,24) :判断今天星期几"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   September 2018\nMo Tu We Th Fr Sa Su\n                1  2\n 3  4  5  6  7  8  9\n10 11 12 13 14 15 16\n17 18 19 20 21 22 23\n24 25 26 27 28 29 30\nTrue\n29\n0\n"
     ]
    }
   ],
   "source": [
    "import calendar\n",
    " \n",
    "# a = calendar.prcal(2018)\n",
    "a = calendar.prmonth(2018,9)\n",
    "# print(a)\n",
    "print(calendar.isleap(2000))\n",
    "print(calendar.leapdays(1900,2018))\n",
    "print(calendar.weekday(2018,9,24))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# time\n",
    "- import time \n",
    "- time.time() : 获取时间戳\n",
    "- time.timezone: 获取本地时间跟UTC时间的差值\n",
    "- time.localtiem() : 获取一个时间元祖，可以通过a.tm_year等来调用\n",
    "- time,asctime() ： 获取一个字符型本地时间\n",
    "- time.mktime(具体时间) ： 把具体时间转换成时间戳\n",
    "- time.sleep(2) : 程序暂停2s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1537773217.390482\n-28800\ntime.struct_time(tm_year=2018, tm_mon=9, tm_mday=24, tm_hour=15, tm_min=13, tm_sec=37, tm_wday=0, tm_yday=267, tm_isdst=0)\nMon Sep 24 15:13:37 2018\n1537773217.0\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "休息一下\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "print(time.time())\n",
    "print(time.timezone)\n",
    "print(time.localtime())\n",
    "print(time.asctime())\n",
    "print(time.mktime(time.localtime()))\n",
    "time.sleep(2)\n",
    "print('休息一下')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# datetime\n",
    "- datetime,datetime,now() : 获取现在的完整时间表示\n",
    "- datatime.timedelta(hours=1) : 代表时间差值为一小时\n",
    "- t.strftime(\"%Y-$m-%f %h-%M\") 按照自己的想法格式化打印时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-09-24 15:40:32.779596\n2018-09-24\n2018-09-25\n1 day, 0:00:00\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime,timedelta\n",
    "# 当前时间的完整表述\n",
    "t = datetime.now()\n",
    "print(t)\n",
    "# 按照要求输出一个时间\n",
    "print(t.strftime(\"%Y-%m-%d\"))\n",
    "# 一个时间差值\n",
    "tt = timedelta(days=1)\n",
    "# 加上一天以后的值\n",
    "print((t+tt).strftime(\"%Y-%m-%d\"))\n",
    "al = t+tt\n",
    "# 两个时间的差值，可以优雅的表示\n",
    "print((al-t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.10057282199704787\n0.06244261000028928\n0.06202180100081023\n"
     ]
    }
   ],
   "source": [
    "# 测试某段代码或者某个函数的运行时间\n",
    "import timeit\n",
    "s = '''\n",
    "def func(num):\n",
    "    sum = []\n",
    "    for i in range(num):\n",
    "        sum.append(i)\n",
    "'''\n",
    "\n",
    "c = '''\n",
    "sum = []\n",
    "num = 4\n",
    "for i in range(num):\n",
    "    sum.append(i)\n",
    "'''\n",
    "t1 = timeit.timeit(stmt=c,number=10_0000)\n",
    "t2 = timeit.timeit(stmt=\"c = [i for i in range(4)]\",number=10_0000)\n",
    "t3 = timeit.timeit(\"func(num)\",setup=s+'num=4',number=10_0000)\n",
    "print(t1)\n",
    "print(t2)\n",
    "print(t3)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
